<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="业主公司名称" prop="entName" label-width="100px">
        <el-input
          v-model="queryParams.entName"
          placeholder="请输入企业名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="企业类型" prop="entType" class="form-item-row">
        <el-select
          v-model="queryParams.entType"
          filterable
          clearable
          placeholder="请输入客户类型"
        >
          <el-option
            v-for="item in enterpriseType"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue"
          ></el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item label="客户类型" prop="clientType" class="form-item-row">
        <el-select
          v-model="queryParams.clientType"
          filterable
          clearable
          placeholder="请输入客户类型"
        >
          <el-option label="普通用户" :value="1"></el-option>
          <el-option label="重点用户" :value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所在城市">
        <provice-select
          @changeValue="queryChangeValue"
          :defaultCity="defaultCity1"
          :isShowDistrict="true"
        ></provice-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['wb:wbEntAuth:add1']"
          >新增</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['wb:wbEntAuth:edit']"
          >修改</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wb:wbEntAuth:remove']"
          >删除</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="wbEntAuthList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="业主公司名称" align="center" prop="entName" />
      <el-table-column label="客户类型" align="center" prop="clientType">
        <template slot-scope="{ row }">
          <span>{{ row.clientType == 1 ? "普通用户" : "重点用户" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="联系人/法人" align="center" prop="contact" />
      <el-table-column label="联系电话" align="center" prop="phoneNumber" />
      <el-table-column label="城市" align="center">
        <template slot-scope="{ row }">
          {{ fomatterAdressName(row.province) }}/{{
            fomatterAdressName(row.city)
          }}/{{ fomatterAdressName(row.county) }}
        </template>
      </el-table-column>
      <el-table-column
        label="维保公司"
        align="center"
        prop="maintainCompanyName"
      />

      <!-- <el-table-column label="详细地址" align="center" prop="entAddress" /> -->
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <!-- <el-table-column label="业主公司类型" align="center" prop="entType">
      </el-table-column> -->
      <!-- <el-table-column label="营业执照" align="center" prop="businessLicense" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.businessLicense" :width="50" :height="50" />
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="描述" align="center" prop="remark" /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="200"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="toDetail(scope.row)"
            v-hasPermi="['wb:wbEntAuth:query']"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['wb:wbEntAuth:edit1']"
            >修改</el-button
          >
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-s-check"
            @click="handleAudit(scope.row)"
            v-hasPermi="['wb:wbEntAuth:edit']"
            :disabled="scope.row.state == 1"
          >审核</el-button> -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wb:wbEntAuth:remove1']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改企业认证对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="100px"
        :inline="true"
        class="dialog-form"
      >
        <el-form-item label="公司名称" prop="entName" class="form-item-row">
          <el-input
            v-model="form.entName"
            placeholder="请输入企业名"
            style="width: 400px"
          />
        </el-form-item>
        <el-form-item label="企业类型" prop="entType" class="form-item-row">
          <el-select
            v-model="form.entType"
            filterable
            clearable
            placeholder="请输入客户类型"
          >
            <el-option
              v-for="item in enterpriseType"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="联系人" prop="contact">
          <el-input v-model="form.contact" placeholder="请输入联系人/法人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phoneNumber">
          <el-input v-model="form.phoneNumber" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="所在地址:" prop="province">
          <provice-select
            :isShowDistrict="true"
            :defaultCity="defaultCity"
            @changeValue="changeValue"
          ></provice-select>
        </el-form-item>
        <el-form-item label="重点单位" prop="clientType">
          <el-radio v-model="form.clientType" :label="2">是</el-radio>
          <el-radio v-model="form.clientType" :label="1">否</el-radio>
          <!-- <el-select v-model="form.clientType" filterable clearable placeholder="请输入客户类型">
            <el-option label="普通用户" :value="1"></el-option>
            <el-option label="重点用户" :value="2"></el-option>
          </el-select> -->
        </el-form-item>
        <el-form-item label="详细地址" prop="entAddress" class="form-item-row">
          <el-input
            v-model="form.entAddress"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="描述" prop="remark" class="form-item-row">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item
          label="营业执照"
          prop="businessLicense"
          class="form-item-row"
        >
          <image-upload v-model="form.businessLicense" :limit="1" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="title != '审核'" type="primary" @click="submitForm"
          >确 定</el-button
        >
        <el-button v-else type="primary" @click="submitForm"
          >审核通过</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <auth-detail
      :showDialog="showDialog"
      :baseInfo="baseInfo"
      @close="showDialog = false"
    ></auth-detail>
  </div>
</template>

<script>
import {
  listWbEntAuth,
  getWbEntAuth,
  delWbEntAuth,
  addWbEntAuth,
  updateWbEntAuth,
  entType,
  listEntAuthWb,
} from "@/api/wb/wbEntAuth";
import ProviceSelect from "@/components/ProviceSelect/index.vue";
import { fomatterAdressName } from "@/utils/index.js";
import { mapGetters } from "vuex";

import AuthDetail from "@/views/components/authDetail/index.vue";
export default {
  name: "WbEntAuth",
  components: { ProviceSelect, AuthDetail },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企业认证表格数据
      wbEntAuthList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        entName: null,
        entType: null,
        contact: null,
        phoneNumber: null,
        province: null,
        provinceString: null,
        city: null,
        cityString: null,
        county: null,
        countyString: null,
        clientType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        entName: [
          { required: true, message: "请输入公司名称", trigger: "blur" },
        ],
        entType: [
          { required: true, message: "请输入企业类型", trigger: "blur" },
        ],
        contact: [{ required: true, message: "请输入联系人", trigger: "blur" }],
        phoneNumber: [
          { required: true, message: "请输入联系电话", trigger: "blur" },
        ],
        clientType: [
          { required: true, message: "请选择重点单位", trigger: "blur" },
        ],

        province: [
          { required: true, message: "请选择地址", trigger: "change" },
        ],
        businessLicense: [
          { required: true, message: "请上传", trigger: "blur" },
        ],
      },
      defaultCity: {
        province: "",
        city: "",
        county: "",
      },
      defaultCity1: {
        province: "",
        city: "",
        county: "",
      },
      showDialog: false,
      baseInfo: {},
      enterpriseType: [],
    };
  },
  computed: {
    ...mapGetters(["allCityList", "provincesList", "cityslist", "regionslist"]),
  },
  created() {
    this.getList();
    this.getEnterpriseType();
  },
  methods: {
    fomatterAdressName,
    /** 查询企业认证列表 */
    getList() {
      this.loading = true;
      listEntAuthWb(this.queryParams).then((response) => {
        this.wbEntAuthList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        entName: null,
        entAddress: null,
        entType: null,
        businessLicense: null,
        contact: null,
        phoneNumber: null,
        remark: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        province: null,
        provinceString: null,
        city: null,
        cityString: null,
        county: null,
        countyString: null,
        clientType: 1,
      };
      this.resetForm("form");
    },
    changeValue(values) {
      this.form.province = values[0];
      this.form.city = values[1];
      this.form.county = values[2];

      this.provincesList.forEach((element) => {
        if (element.id == this.form.province) {
          this.form.provinceString = element.cname;
        }
      });

      this.cityslist.forEach((element) => {
        if (element.id == this.form.city) {
          this.form.cityString = element.cname;
        }
      });

      this.allCityList.forEach((element) => {
        if (element.id == this.form.county) {
          this.form.countyString = element.cname;
        }
      });
    },
    queryChangeValue(values) {
      console.log(values);
      this.queryParams.province = values[0];
      this.queryParams.city = values[1];
      this.queryParams.county = values[2];
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.defaultCity1 = {
        province: "",
        city: "",
        county: "",
      };
      this.queryParams.province = "";
      this.queryParams.city = "";
      this.queryParams.county = "";

      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.defaultCity = {
        province: "",
        city: "",
        county: "",
      };
      this.queryParams.province = "";
      this.queryParams.city = "";
      this.queryParams.county = "";

      this.reset();
      this.open = true;
      this.title = "添加企业认证";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getWbEntAuth(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改企业认证";
        this.defaultCity = {
          province: response.data.province ? response.data.province + "" : "",
          city: response.data.city ? response.data.city + "" : "",
          district: response.data.county ? response.data.county + "" : "",
        };
      });
    },
    // 获取企业类型
    getEnterpriseType() {
      entType().then((res) => {
        this.enterpriseType = res.data;
      });
    },
    handleAudit(row) {
      this.reset();
      const id = row.id || this.ids;
      getWbEntAuth(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "审核";
        this.defaultCity = {
          province: response.province,
          city: response.city,
          district: response.county,
        };
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            let params = {};
            if (this.title == "审核") {
              params = {
                ...this.form,
                state: 1,
              };
            } else {
              params = {
                ...this.form,
              };
            }
            console.log(params);
            updateWbEntAuth(params).then((response) => {
              this.$modal.msgSuccess(
                this.title == "审核" ? "审核成功" : "修改成功"
              );
              this.open = false;
              this.getList();
            });
          } else {
            addWbEntAuth(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除企业认证编号为"' + ids + '"的数据项？')
        .then(function () {
          return delWbEntAuth(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "wb/wbEntAuth/export",
        {
          ...this.queryParams,
        },
        `wbEntAuth_${new Date().getTime()}.xlsx`
      );
    },
    toDetail(row) {
      this.showDialog = true;
      this.baseInfo = { ...row };
    },
  },
};
</script>
